﻿/*
* Copyright 2010 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ngilead.Test.Models;
using NHibernate;
using System.Collections;
using Ngilead.Test.DAO;
using NHibernate.Criterion;
using Ngilead.Test.Models.Stateless;

namespace Ngilead.Test.Services
{
    /// <summary>
    /// Message data access class
    /// </summary>
    public class MessageService : IMessageService
    {
        #region DAO

        private IMessageDAO _messageDAO;

        /// <summary>
        /// Message DAO
        /// </summary>
        private IMessageDAO MessageDAO
        {
            get
            {
                if (_messageDAO == null)
                    _messageDAO = DAOFactory.MessageDAO;
                return _messageDAO;
            }
        }

        #endregion

        #region Generic

        /// <summary>
        /// Get message by id
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns>Message</returns>
        public Message LoadById(Int64 id)
        {
            return (Message)MessageDAO.LoadById(id).Clone();
        }

        /// <summary>
        /// Load all messages by id
        /// </summary>
        /// <returns>All messages</returns>
        public IList<Message> LoadAll()
        {
            return (IList<Message>)MessageDAO.LoadAll().Clone();
        }

        /// <summary>
        /// Count all messages
        /// </summary>
        /// <returns>Count</returns>
        public Int64 CountAll()
        {
            return MessageDAO.CountAll();
        }

        /// <summary>
        /// Save or update message
        /// </summary>
        /// <param name="obj">Message</param>
        public void Save(Message obj)
        {
            obj = (Message)obj.Merge();
            MessageDAO.Save(obj);
        }

        /// <summary>
        /// Delete message
        /// </summary>
        /// <param name="obj">Message</param>
        public void Delete(Message obj)
        {
            obj = (Message)obj.Merge();
            MessageDAO.Delete(obj);
        }

        #endregion

        /// <summary>
        /// Load last message
        /// </summary>
        /// <returns>Message</returns>
        public Message LoadLastMessage()
        {
            return (Message)MessageDAO.LoadLastMessage().Clone();
        }

        /// <summary>
        /// Load all messages
        /// </summary>
        /// <param name="startIndex">Start index</param>
        /// <param name="maxResult">Max result</param>
        /// <returns>Messages</returns>
        public IList<Message> LoadAllMessage(int startIndex, int maxResult)
        {
            return (IList<Message>)MessageDAO.LoadAllMessage(startIndex, maxResult).Clone();
        }

        /// <summary>
        /// Load detailed message 
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns>Message</returns>
        public Message LoadDetailedMessage(Int64 id)
        {
            return (Message)MessageDAO.LoadDetailedMessage(id).Clone();
        }

        /// <summary>
        /// Lock message
        /// </summary>
        /// <param name="session">Session</param>
        /// <param name="message">Message</param>
        public void LockMessage(ISession session, Message message)
        {
            MessageDAO.LockMessage(session, message);
        }
    }
}